package com.halixun.d2app.ui;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.halixun.d2app.MainActivity;
import com.halixun.d2app.R;
import com.halixun.d2app.base.BaseActivity;
import com.halixun.d2app.base.BaseResult;
import com.halixun.d2app.bean.ClientUserInfo;
import com.halixun.d2app.net.CreateRequestEntity;
import com.halixun.d2app.net.DataState;
import com.halixun.d2app.net.errer.ResultVerifier;
import com.halixun.d2app.utils.RequestDataUtils;
import com.halixun.d2app.utils.SystemUtil;
import com.halixun.d2app.utils.ToastUtils;
import com.halixun.d2app.utils.VersionUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends BaseActivity {

    private EditText accountNumber, password;

    private TextView loginBtn;

    private ViewGroup accountLayout, passwordLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        hideActionBar();
        initView();
        setListeners();
    }

    private void setListeners() {
        loginBtn.setOnClickListener(this);
        accountNumber.setOnFocusChangeListener((v, hasFocus) ->
                accountLayout.setBackgroundResource(hasFocus ? R.drawable.login_input_selected_shape : R.drawable.login_input_unselected_shape));
        password.setOnFocusChangeListener((v, hasFocus) ->
                passwordLayout.setBackgroundResource(hasFocus ? R.drawable.login_input_selected_shape : R.drawable.login_input_unselected_shape));
    }

    private void initView() {
        accountNumber = findViewById(R.id.accountNumber);
        password = findViewById(R.id.password);
        loginBtn = findViewById(R.id.loginBtn);

        accountLayout = findViewById(R.id.accountLayout);
        passwordLayout = findViewById(R.id.passwordLayout);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.loginBtn:
                login();
                break;
        }
    }

    @SuppressLint({"CheckResult", "NewApi"})
    private void login() {
        String account = accountNumber.getText().toString().trim();
        String passwordNum = password.getText().toString().trim();
        if (TextUtils.isEmpty(account)) {
            ToastUtils.showBottomToast("请输入账号");
            return;
        }
        if (TextUtils.isEmpty(passwordNum)) {
            ToastUtils.showBottomToast("请输入密码");
            return;
        }
        showProgressDialog("登陆中...");
        JsonObject object = new JsonObject();
        object.addProperty("siteUserId", account);
        object.addProperty("password", passwordNum);
        RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), object.toString());
        CreateRequestEntity.getWebService().login(body, RequestDataUtils.getRequestDataParam(this)).enqueue(new Callback<BaseResult<ClientUserInfo>>() {
            @Override
            public void onResponse(Call<BaseResult<ClientUserInfo>> call, Response<BaseResult<ClientUserInfo>> response) {
                dismissProgressDialog();
                if (ResultVerifier.isSucceed(response)) {
                    DataState.getInstance().appLogin(response.body().Data);
                    CreateRequestEntity.isCreateWebService = true;
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();
                } else {
                    ResultVerifier.serceErrers(response);
                }
            }

            @Override
            public void onFailure(Call<BaseResult<ClientUserInfo>> call, Throwable t) {
                dismissProgressDialog();
                ResultVerifier.systerErrers(t);
            }
        });
    }

}
